package service.impl;

import dao.UserDao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import service.UserService;

import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/5/19 13:41
 * Version:1.0-SNAPSHOT
 * Description:
 */
// @Transactional注解用于配置事务
// 该注解可以配置在类上,也可以配置在方法上
// 当配置在类上,表示当前类中所有的方法均使用该配置
// 当配置在方法上,表示只有当前方法使用该配置
// 如果类上与方法上同时存在,则优先使用方法上的配置
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class,noRollbackFor = ArithmeticException.class,timeout = 3)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<User> findAll() {
        return userDao.selectAll();
    }

    @Override
    public void addUser(String username, String password, String phone, String address) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        User user = new User(username,password,phone,address);
        userDao.insertUser(user);
//        int i = 1/0;
    }
}
